window.onload = initalizePage;
window.onunload = function() {}
var xhr = false;
var img = false;
var body = false;

function initalizePage() {
	var select_sort = document.getElementById('sort');
	select_sort.onchange = function() { updateProducts('sort', this.value); }
	var select_itemPerPage = document.getElementById('itemNum');
	select_itemPerPage.onchange = function() { updateProducts('per_page', this.value); }


	var select_sort2 = document.getElementById('sort2');
	select_sort2.onchange = function() { window.location.search = generate_search_str('sort', this.value); };
	var select_itemPerPage2 = document.getElementById('itemNum2');
	select_itemPerPage2.onchange = function() { window.location.search = generate_search_str('per_page', this.value); };
}

function moveHandler(e) {
	var posx = 0;
	var posy = 0;
	if (!e) { var e = window.event; }
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	img.style.top = (posy - 20) + 'px';
	img.style.left = (posx + 10) + 'px';
}



function updateProducts(urlOpt, newVal) {
	if (getXhr()) {
		var productsDiv = document.getElementById('products');
		var url = window.location.pathname + generate_search_str(urlOpt, newVal) + '&reqType=ajax';

		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var resXML = xhr.responseText;
					//var resXML = xhr.responseXML;
/*
					var productsX = resXML.getElementsByTagName('product')
					for (var i = 0; i < productsX.length; i++) {
						//document.write(productsX[i].getElementsByTagName('price')[0].firstChild.nodeValue);
						//document.write(productsX[i].getAttribute('id'));
					}
*/
					productsDiv.innerHTML = resXML;
					productsDiv.style.opacity = 1;
					window.onmousemove = false;
					img.style.display = 'none';
				}
				else {
					var outMsg = "Problem with the request " + xhr.status;
				}
			}
			else {
				productsDiv.style.opacity = 0.3;
				if (!img) {
					img = document.createElement('img');
					img.src = '/24/only_2_m/javascripts/public/ax.gif';
					img.style.position = 'absolute';
					body = document.getElementsByTagName('body')[0];
					body.appendChild(img);
				}
				else {
					img.style.display = 'block';
				}
				window.onmousemove = moveHandler;
			}
		}
		xhr.open('GET', url, true);
		xhr.send(null);
	}
	else {
		alert("Sorry, couldn't create an XMLHttpRequest");
	}
}


function generate_search_str(urlOpt, newVal) {
	search_arr = window.location.search.substring(1).split('&');

	var niceArr = [];
	for (var intgr in search_arr) {
		var keyVal = search_arr[intgr].split('=');
		niceArr[keyVal[0]] = keyVal[1];
	}
	niceArr[urlOpt] = newVal;

	var out_search_arr = []
	for (var key in niceArr) {
		var temp_str = key + '=' + niceArr[key];
		out_search_arr.push(temp_str);
	}
	return ('?' + out_search_arr.join('&'));
}


// ----------------------------------------------------------

function sendRequest(url,callback,postData) {
	var req = getXhr();
	if (!req) { return; }
	var method = (postData) ? 'POST' : 'GET';
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData) { req.setRequestHeader('Content-type','application/x-www-form-urlencoded'); }
	req.onreadystatechange = function () {
		if (req.readyState != 4) { return; }
		if (req.status != 200 && req.status != 304) {
			alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) { return; }
	req.send(postData);
}


var XMLHttpPossibilities = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function getXhr() {
	for (var i=0;i<XMLHttpPossibilities.length;i++) {
		try { xhr = XMLHttpPossibilities[i](); }
		catch (e) {	continue; }
		break;
	}
	return xhr;
}

// ----------------------------------------------------------